package org.jeecg.modules.pos.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.pos.entity.PosOrderGoodsDetail;
import org.jeecg.modules.pos.entity.PosSellClearGoods;
import org.jeecg.modules.pos.entity.PosThali;
import org.jeecg.modules.pos.service.IPosOrderGoodsDetailService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.pos.service.IPosThaliService;
import org.jeecg.modules.rooms.entity.CesGoods;
import org.jeecg.modules.rooms.service.CesGoodsServiceImpl;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: pos_order_goods_detail
 * @Author: jeecg-boot
 * @Date:   2023-04-13
 * @Version: V1.0
 */
@Api(tags="pos_order_goods_detail")
@RestController
@RequestMapping("/pos/posOrderGoodsDetail")
@Slf4j
public class PosOrderGoodsDetailController extends JeecgController<PosOrderGoodsDetail, IPosOrderGoodsDetailService> {
	@Autowired
	private IPosOrderGoodsDetailService posOrderGoodsDetailService;
@Autowired
private CesGoodsServiceImpl cesGoodsService;
@Resource
private IPosThaliService posThaliService;
	/**
	 * 分页列表查询
	 */
	//@AutoLog(value = "pos_order_goods_detail-分页列表查询")
	@ApiOperation(value="pos_order_goods_detail-分页列表查询", notes="pos_order_goods_detail-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<PosOrderGoodsDetail>> queryPageList(PosOrderGoodsDetail posOrderGoodsDetail,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PosOrderGoodsDetail> queryWrapper = QueryGenerator.initQueryWrapper(posOrderGoodsDetail, req.getParameterMap());
		Page<PosOrderGoodsDetail> page = new Page<PosOrderGoodsDetail>(pageNo, pageSize);
		IPage<PosOrderGoodsDetail> pageList = posOrderGoodsDetailService.page(page, queryWrapper);
		pageList.getRecords().forEach(item -> {
			CesGoods cesGoods = cesGoodsService.getById(item.getGoodsId());
			if (cesGoods != null) {
				item.setGoodsName(cesGoods.getName());
			} else {
				PosThali posThali = posThaliService.getById(item.getGoodsId());
				if (posThali != null) {
					item.setGoodsName(posThali.getName());
				}
			}
		});
		return Result.OK(pageList);
	}

	 /**
	  * 通过订单id查询订单详情
	  */
	 //@AutoLog(value = "pos_order_goods_detail-分页列表查询")
	 @ApiOperation(value="pos_order_goods_detail-通过订单id查询订单详情", notes="pos_order_goods_detail-通过订单id查询订单详情")
	 @GetMapping(value = "/getDetailByOrderId")
	 public Result<List<PosOrderGoodsDetail>> getDetailByOrderId(PosOrderGoodsDetail posOrderGoodsDetail,HttpServletRequest req) {
		 QueryWrapper<PosOrderGoodsDetail> queryWrapper = QueryGenerator.initQueryWrapper(posOrderGoodsDetail, req.getParameterMap());
		 List<PosOrderGoodsDetail> orderGoodsDetailList = posOrderGoodsDetailService.list(queryWrapper);
		 List<PosOrderGoodsDetail> result = new ArrayList<>();
		 orderGoodsDetailList.forEach(item -> {
			 CesGoods cesGoods = cesGoodsService.getById(item.getGoodsId());
			 if (cesGoods != null) {
				 item.setGoodsName(cesGoods.getName());
			 }
			 if (StrUtil.isNotEmpty(item.getThaliId()) && StrUtil.isEmpty(item.getParentId())) {
				 PosThali posThali = posThaliService.getById(item.getThaliId());
				 if (posThali == null){
					 throw new JeecgBootException("未找到商品");
				 }
				 item.setGoodsName(posThali.getName());
			 }
			 if (StrUtil.isEmpty(item.getParentId())){
				 result.add(item);
			 }
		 });
		 result.forEach(e -> {
			 List<PosOrderGoodsDetail> child = orderGoodsDetailList.stream().filter(ele -> e.getId()
					 .equals(ele.getParentId())).collect(Collectors.toList());
			 e.setChildDetails(child);
		 });
		 return Result.OK(result);
	 }

	/**
	 *   添加
	 *
	 * @param posOrderGoodsDetail
	 * @return
	 */
	@AutoLog(value = "pos_order_goods_detail-添加")
	@ApiOperation(value="pos_order_goods_detail-添加", notes="pos_order_goods_detail-添加")
	//@RequiresPermissions("pos:pos_order_goods_detail:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody PosOrderGoodsDetail posOrderGoodsDetail) {
		posOrderGoodsDetailService.save(posOrderGoodsDetail);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param posOrderGoodsDetail
	 * @return
	 */
	@AutoLog(value = "pos_order_goods_detail-编辑")
	@ApiOperation(value="pos_order_goods_detail-编辑", notes="pos_order_goods_detail-编辑")
	//@RequiresPermissions("pos:pos_order_goods_detail:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody PosOrderGoodsDetail posOrderGoodsDetail) {
		posOrderGoodsDetailService.updateById(posOrderGoodsDetail);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "pos_order_goods_detail-通过id删除")
	@ApiOperation(value="pos_order_goods_detail-通过id删除", notes="pos_order_goods_detail-通过id删除")
	//@RequiresPermissions("pos:pos_order_goods_detail:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		posOrderGoodsDetailService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "pos_order_goods_detail-批量删除")
	@ApiOperation(value="pos_order_goods_detail-批量删除", notes="pos_order_goods_detail-批量删除")
	//@RequiresPermissions("pos:pos_order_goods_detail:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.posOrderGoodsDetailService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "pos_order_goods_detail-通过id查询")
	@ApiOperation(value="pos_order_goods_detail-通过id查询", notes="pos_order_goods_detail-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<PosOrderGoodsDetail> queryById(@RequestParam(name="id",required=true) String id) {
		PosOrderGoodsDetail posOrderGoodsDetail = posOrderGoodsDetailService.getById(id);
		if(posOrderGoodsDetail==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(posOrderGoodsDetail);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param posOrderGoodsDetail
    */
    //@RequiresPermissions("pos:pos_order_goods_detail:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PosOrderGoodsDetail posOrderGoodsDetail) {
        return super.exportXls(request, posOrderGoodsDetail, PosOrderGoodsDetail.class, "pos_order_goods_detail");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("pos:pos_order_goods_detail:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PosOrderGoodsDetail.class);
    }

}
